package com.ithanwen.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ithanwen.mapper.StudentMapper;
import com.ithanwen.pojo.PageResult;
import com.ithanwen.pojo.Student;
import com.ithanwen.pojo.StudentQueryParam;
import com.ithanwen.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Queue;

/**
 * Created By tony
 * <br/>Date: 2025/7/14 16:58
 * <br/>Description:
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult page(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        List<Student> studentList = studentMapper.search(studentQueryParam.getName(), studentQueryParam.getDegree(), studentQueryParam.getClazzId());
        PageResult studentPage = new PageResult(new PageInfo<Student>(studentList).getTotal(),studentList);
        return studentPage;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    @Override
    public Student findById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public void update(Student student) {
        studentMapper.updateById(student);
    }

    @Override
    public void updateViolation(Integer id, Short score) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Student student = studentMapper.selectOne(queryWrapper);
        student.setViolationScore(score);
        student.setViolationCount((short) (student.getViolationCount() + 1));
        studentMapper.updateById(student);
    }


}
